package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallWalletRecordMapper;
import org.linlinjava.litemall.db.domain.LitemallWalletRecord;
import org.linlinjava.litemall.db.domain.LitemallWalletRecordExample;
import org.linlinjava.litemall.db.domain.LitemallWalletRecordVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallWalletRecordService {
    @Resource
    private LitemallWalletRecordMapper recordMapper;

    public int add(LitemallWalletRecord record) {
       return recordMapper.insertSelective(record);
    }

    public List<LitemallWalletRecordVo> queryWalletRecord(Integer mechanismId, Integer page, Integer size, String sort, String order) {

        PageHelper.startPage(page, size);
        return recordMapper.queryWalletRecordMapper(mechanismId);
    }
    public List<LitemallWalletRecordVo> queryWalletRecordBills(Integer mechanismId,LocalDateTime start, LocalDateTime end){
        return recordMapper.queryWalletRecordMapperBills(mechanismId,start,end);
    }
    public List<LitemallWalletRecord> queyByOrgId(Integer id){
        LitemallWalletRecordExample litemallWalletRecordExample = new LitemallWalletRecordExample();
        litemallWalletRecordExample.or().andOrgIdEqualTo(id).andPayIdIsNotNull();
        return recordMapper.selectByExample(litemallWalletRecordExample);
    }
    public List<LitemallWalletRecord> querySelectiveByUserId(Integer userId, Integer page, Integer size, String sort, String order) {
        LitemallWalletRecordExample example = new LitemallWalletRecordExample();
        LitemallWalletRecordExample.Criteria criteria = example.createCriteria().andPayIdIsNotNull().andPayTimeIsNotNull();
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        example.orderBy(LitemallWalletRecord.Column.modifyTime.desc());
        PageHelper.startPage(page, size);
        return recordMapper.selectByExample(example);
    }
    public List<LitemallWalletRecord> querySelectiveByMerchaismId(Integer userId, LocalDateTime time,LocalDateTime time2,Byte type, Integer page, Integer size) {
        LitemallWalletRecordExample example = new LitemallWalletRecordExample();
        LitemallWalletRecordExample.Criteria criteria = example.createCriteria().andModifyTimeIsNotNull();
        if (!StringUtils.isEmpty(userId)) {
            criteria.andMechaismIdEqualTo(userId);
        }
        if(time!=null){
            criteria.andModifyTimeBetween(time,time2);
        }
        if(type!=null){
            criteria.andTypeEqualTo(type);
        }
        example.orderBy(LitemallWalletRecord.Column.modifyTime.desc());
        PageHelper.startPage(page, size);
        return recordMapper.selectByExample(example);
    }

    public int countelectiveByUserId(Integer userId, Integer page, Integer size, String sort, String order) {
        LitemallWalletRecordExample example = new LitemallWalletRecordExample();
        LitemallWalletRecordExample.Criteria criteria = example.createCriteria().andPayIdIsNotNull().andPayTimeIsNotNull();
        if (!StringUtils.isEmpty(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        example.orderBy(LitemallWalletRecord.Column.modifyTime.desc());
        PageHelper.startPage(page, size);
        return (int)recordMapper.countByExample(example);
    }

    public int countelectiveByMerchaismId(Integer userId, LocalDateTime time,LocalDateTime time2,Byte type, Integer page, Integer size) {
        LitemallWalletRecordExample example = new LitemallWalletRecordExample();
        LitemallWalletRecordExample.Criteria criteria = example.createCriteria().andModifyTimeIsNotNull();
        if (!StringUtils.isEmpty(userId)) {
            criteria.andMechaismIdEqualTo(userId);
        }
        if(time!=null){
            criteria.andModifyTimeBetween(time,time2);
        }
        if(type!=null){
            criteria.andTypeEqualTo(type);
        }
        example.orderBy(LitemallWalletRecord.Column.modifyTime.desc());
        PageHelper.startPage(page, size);
        return (int)recordMapper.countByExample(example);
    }

    public int countelective(Integer mechanismId, Integer page, Integer size, String sort, String order) {
        LitemallWalletRecordExample example = new LitemallWalletRecordExample();
        example.orderBy(LitemallWalletRecord.Column.modifyTime.desc());
        LitemallWalletRecordExample.Criteria criteria = example.createCriteria().andPayIdIsNotNull();
        if (!StringUtils.isEmpty(mechanismId)) {
            criteria.andModifyResourceEqualTo(mechanismId);
        }
        byte a = 1;
        criteria.andTypeEqualTo(a);
        PageHelper.startPage(page, size);
        return (int)recordMapper.countByExample(example);
    }

    public LitemallWalletRecord queryByRecordSn(String recordSn) {
        LitemallWalletRecordExample example = new LitemallWalletRecordExample();
        LitemallWalletRecordExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(recordSn)) {
            criteria.andRecordSnEqualTo(recordSn);
        }
        List<LitemallWalletRecord> walletRecordList = recordMapper.selectByExample(example);
        if (walletRecordList.size() == 1) {
            return walletRecordList.get(0);
        } else {
            return null;
        }
    }

    public LitemallWalletRecord queryRecordSn(String recordSn) {
        LitemallWalletRecordExample example = new LitemallWalletRecordExample();
        example.or().andRecordSnEqualTo(recordSn);
        return recordMapper.selectOneByExample(example);
    }

    public LitemallWalletRecord queryById(Integer id) {
       return recordMapper.selectByPrimaryKey(id);
    }
    public void update(LitemallWalletRecord litemallWalletRecord) {
        recordMapper.updateByPrimaryKeySelective(litemallWalletRecord);
    }

    public void updatePay(Integer id) {
        recordMapper.updatePayByExample(id);
    }

    public void updateUserid(Integer oldId ,Integer id) {
        recordMapper.updateUseridExample(oldId,id);
    }

    public LitemallWalletRecord queryByOrgId(Integer id){
        LitemallWalletRecordExample example = new LitemallWalletRecordExample();
        example.or().andOrgIdEqualTo(id);
        return recordMapper.selectOneByExample(example);
    }
}
